﻿
// FIXME works for View2D only

/// <reference path='ol3/ol-whitespace.js' />



goog.provide('ol.control.LiuGForm');

goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('ol.control.Control');
goog.require('ol.css');
goog.require('ol.pointer.PointerEventHandler');



ol.control.LiuGForm = function (opt_options)
{
    var options = goog.isDef(opt_options) ? opt_options : {};

    this.Width = goog.isDef(options.width) ? options.width : 180;
    this.Height = goog.isDef(options.height) ? options.height : 128;

    this.dragMinWidth = goog.isDef(options.minWidth) ? options.minWidth : 180;
    this.dragMinHeight = goog.isDef(options.minHeight) ? options.minHeight : 128;

    var locTop = goog.isDef(options.top) ? options.top : 50;
    var locRight = goog.isDef(options.right) ? options.right : 10;


    var className = goog.isDef(options.className) ? options.className :
        'ol-zoom-extent';

    var dragstyle = 'width:' + this.Width + 'px;' + 'height:' + this.Height + 'px;'+
        'top:' + locTop + 'px;' + 'right:' + locRight + 'px;';
    var dragElement = goog.dom.createDom(goog.dom.TagName.DIV,
        {
            'id': 'drag','style':dragstyle
        });

    var title = goog.dom.createDom(goog.dom.TagName.DIV,
    {
        'id': 'title', 'class': 'title'
    });

    var titleName = goog.isDef(options.title) ? options.title : "Form1";
    var titlecontent = goog.dom.createDom(goog.dom.TagName.h3, {}, titleName);
    goog.dom.appendChild(title, titlecontent);


    var formbutton = goog.dom.createDom(goog.dom.TagName.DIV,
    {
        'id':'formbuttons'
    });
    var closebutton = goog.dom.createDom(goog.dom.TagName.A,
    {
        'id': 'btn_close', 'class': 'close', 'href': 'javascript:;', 'title': '关闭'
    });
    var resizeL = goog.dom.createDom(goog.dom.TagName.DIV,
    {
        'id': 'resizeL', 'class': 'resizeL'
    });
    var resizeT = goog.dom.createDom(goog.dom.TagName.DIV,
    {
        'id': 'resizeT', 'class': 'resizeT'
    });
    var resizeR = goog.dom.createDom(goog.dom.TagName.DIV,
    {
        'id': 'resizeR', 'class': 'resizeR'
    });
    var resizeB = goog.dom.createDom(goog.dom.TagName.DIV,
    {
        'id': 'resizeB', 'class': 'resizeB'
    });
    var resizeLT = goog.dom.createDom(goog.dom.TagName.DIV,
    {
        'id': 'resizeLT', 'class': 'resizeLT'
    });
    var resizeTR = goog.dom.createDom(goog.dom.TagName.DIV,
    {
        'id': 'resizeTR', 'class': 'resizeTR'
    });

    var resizeBR = goog.dom.createDom(goog.dom.TagName.DIV,
    {
    'id': 'resizeBR', 'class': 'resizeBR'
    });
    var resizeLB = goog.dom.createDom(goog.dom.TagName.DIV,
    {
        'id': 'resizeLB', 'class': 'resizeLB'
    });

    var content = goog.dom.createDom(goog.dom.TagName.DIV,
    {
        'id': 'content', 'class': 'content'
    });

    goog.dom.appendChild(formbutton, closebutton);
    goog.dom.appendChild(title, formbutton);
    goog.dom.appendChild(dragElement, title);

    goog.dom.appendChild(dragElement, resizeL);
    goog.dom.appendChild(dragElement, resizeT);
    goog.dom.appendChild(dragElement, resizeR);
    goog.dom.appendChild(dragElement, resizeB);
    goog.dom.appendChild(dragElement, resizeLT);
    goog.dom.appendChild(dragElement, resizeTR);
    goog.dom.appendChild(dragElement, resizeBR);
    goog.dom.appendChild(dragElement, resizeLB);
    goog.dom.appendChild(dragElement, content);


    this.drag(dragElement, title);
    //四角
    this.resize(dragElement, resizeLT, true, true, false, false,this.dragMinWidth,this.dragMinHeight);
    this.resize(dragElement, resizeTR, false, true, false, false, this.dragMinWidth, this.dragMinHeight);
    this.resize(dragElement, resizeBR, false, false, false, false, this.dragMinWidth, this.dragMinHeight);
    this.resize(dragElement, resizeLB, true, false, false, false, this.dragMinWidth, this.dragMinHeight);
    //四边
    this.resize(dragElement, resizeL, true, false, false, true, this.dragMinWidth, this.dragMinHeight);
    this.resize(dragElement, resizeT, false, true, true, false, this.dragMinWidth, this.dragMinHeight);
    this.resize(dragElement, resizeR, false, false, false, true, this.dragMinWidth, this.dragMinHeight);
    this.resize(dragElement, resizeB, false, false, true, false, this.dragMinWidth, this.dragMinHeight);

    //dragElement.style.left = (document.documentElement.clientWidth - oDrag.offsetWidth) / 2 + "px";
    //dragElement.style.top = (document.documentElement.clientHeight - oDrag.offsetHeight) / 2 + "px";

    goog.events.listen(closebutton, goog.events.EventType.CLICK,this.buttonAction, false,this);


    if (goog.isDef(options.element))
    {
        goog.dom.appendChild(content, options.element);
    }

    goog.base(this, {
        element: dragElement,
        target: options.target
    });
};
goog.inherits(ol.control.LiuGForm, ol.control.Control);

 ol.control.LiuGForm.prototype.drag=function(oDrag, handle)
{
    var disX = dixY = 0;
    var oClose = $("#btn_close");
    handle = handle || oDrag;
    handle.style.cursor = "move";
    handle.onmousedown = function (event)
    {
        var event = event || window.event;
        disX = event.clientX - oDrag.offsetLeft;
        disY = event.clientY - oDrag.offsetTop;

        document.onmousemove = function (event)
        {
            var event = event || window.event;
            var iL = event.clientX - disX;
            var iT = event.clientY - disY;
            var maxL = document.documentElement.clientWidth - oDrag.offsetWidth;
            var maxT = document.documentElement.clientHeight - oDrag.offsetHeight;

            iL <= 0 && (iL = 0);
            iT <= 0 && (iT = 0);
            iL >= maxL && (iL = maxL);
            iT >= maxT && (iT = maxT);

            oDrag.style.left = iL + "px";
            oDrag.style.top = iT + "px";

            return false
        };

        document.onmouseup = function ()
        {
            document.onmousemove = null;
            document.onmouseup = null;
            this.releaseCapture && this.releaseCapture()
        };
        this.setCapture && this.setCapture();
        return false
    };
   
}
/*-------------------------- +
  改变大小函数
 +-------------------------- */
 ol.control.LiuGForm.prototype.resize=function(oParent, handle, isLeft, isTop, lockX, lockY,minWidth,minHeight)
{
    handle.onmousedown = function (event)
    {
        var event = event || window.event;
        var disX = event.clientX - handle.offsetLeft;
        var disY = event.clientY - handle.offsetTop;
        var iParentTop = oParent.offsetTop;
        var iParentLeft = oParent.offsetLeft;
        var iParentWidth = oParent.offsetWidth;
        var iParentHeight = oParent.offsetHeight;

        document.onmousemove = function (event)
        {
            var event = event || window.event;

            var iL = event.clientX - disX;
            var iT = event.clientY - disY;
            var maxW = document.documentElement.clientWidth - oParent.offsetLeft - 2;
            var maxH = document.documentElement.clientHeight - oParent.offsetTop - 2;
            var iW = isLeft ? iParentWidth - iL : handle.offsetWidth + iL;
            var iH = isTop ? iParentHeight - iT : handle.offsetHeight + iT;

            isLeft && (oParent.style.left = iParentLeft + iL + "px");
            isTop && (oParent.style.top = iParentTop + iT + "px");

            iW < minWidth && (iW = minWidth);
            iW > maxW && (iW = maxW);
            lockX || (oParent.style.width = iW + "px");

            iH < minHeight && (iH = minHeight);
            iH > maxH && (iH = maxH);
            lockY || (oParent.style.height = iH + "px");

            if ((isLeft && iW == minWidth) || (isTop && iH == minHeight)) document.onmousemove = null;


            /// 改变container的大小
            //var conpanel = document.getElementById("content");
            //var stylep = "height:" + (iH - 10) + "px; width:"+(iW-10)+"px;";
            //conpanel.style = stylep;



            return false;
        };
        document.onmouseup = function ()
        {
            document.onmousemove = null;
            document.onmouseup = null;
        };
        return false;
    }
};


 ol.control.LiuGForm.prototype.buttonAction=function()
 {
     //this.element.parentNode.removeChild(this.element);
     this.disposeInternal();
 }


